<?php

error_reporting(E_STRICT | E_ALL);
date_default_timezone_set('America/New_York');
ini_set('display_errors', 1);
set_time_limit(60*30);

require_once "config.php";
require_once dirname(__FILE__). '/util/connection.class.php';

$dbconn = Connection::getInstance();
$alertemails = array('sendfrom' => 'no-reply@cashdazzle.com',
					 'sendtoArray' => array('dawei@infinitenine.com',
											'dawei.sun@gmail.com',
											'tony@infinitenine.com',
											'arthur@casualgamingbrands.com'
									  )
					);

$alertUsers = getAlertUsers($dbconn);
if(count($alertUsers) > 0){
	$hasAlertedUsers = getHasAlertedUsers($dbconn);
	$afterFilteredAlterUsers = array_diff($alertUsers, $hasAlertedUsers);
	
	if(count($afterFilteredAlterUsers)>0){
		array_multisort($afterFilteredAlterUsers);
		if(!empty($alertemails['sendtoArray'])){
			foreach($alertemails['sendtoArray'] as $sendto){
				addAlertEmailToDb($alertemails['sendfrom'], $sendto, $afterFilteredAlterUsers, $dbconn);
			}
		}
		saveAlertUsers($afterFilteredAlterUsers, $dbconn);
	}
}



function getAlertUsers(Connection $db)
{
	$sql = "SELECT id FROM users WHERE daily>=20";
	try{
		$alertUsers = $db->query($sql);
		foreach($alertUsers as $k=>$v){
			$alertUsers[$k] = $v['id'];
		}
		return $alertUsers;
	}catch (Exception $e){
		QLog::log(__CLASS__.' '.__METHOD__.' --- '.$e->getMessage(), QLog::ERR);
		throw new Model_Exception($e->getMessage(), $e->getCode());
	}
}

function addAlertEmailToDb($sendfrom, $sendto, $alertUsers, Connection $db){
	$sql = "insert into emails set sender=?, sendfrom=?, subject=?, sendto=?, content=?, created=?, sent=?, priority=? ";
	try{
		$content = 'Someone has won more than $20 today! Here is the id list of the users:' . implode(', ', $alertUsers);
		return $db->execute($sql, array('CGB Alert', $sendfrom, 'Alert: Someone won $20 today', $sendto, $content, time(), 0, 1000));
	}catch (Exception $e){
		QLog::log(__CLASS__.' '.__METHOD__.' --- '.$e->getMessage(), QLog::ERR);
		throw new Model_Exception($e->getMessage(), $e->getCode());
	}
}

function getHasAlertedUsers(Connection $db)
{
	$sql = " SELECT users_id FROM daily_alerted WHERE date=? ";
	try{
		$result = $db->query($sql, array( date('Y-m-d') ));
		if(count($result)>0 && $result[0]['users_id'] != ''){
			return unserialize($result[0]['users_id']);
		}
		
		return array();
	}catch (Exception $e){
		QLog::log(__CLASS__.' '.__METHOD__.' --- '.$e->getMessage(), QLog::ERR);
		throw new Model_Exception($e->getMessage(), $e->getCode());
	}
}

function saveAlertUsers($alertUsers, Connection $db)
{
	$hasAlertedUsers = getHasAlertedUsers($db);
	$insert_users_id = array();
	if(!empty($hasAlertedUsers)){
		$sql = " Update daily_alerted SET users_id=? WHERE date=? ";
		$insert_users_id = array_merge($alertUsers, $hasAlertedUsers);
	}else{
		$sql = " INSERT INTO daily_alerted SET users_id=?, date=? ";
		$insert_users_id = $alertUsers;
	}
	
	try{
		return $db->execute($sql, array( serialize($insert_users_id), date('Y-m-d') ));
	}catch (Exception $e){
		QLog::log(__CLASS__.' '.__METHOD__.' --- '.$e->getMessage(), QLog::ERR);
		throw new Model_Exception($e->getMessage(), $e->getCode());
	}
}


















